package com.woniuxy.provider.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.provider.model.entity.Stu;
import com.woniuxy.provider.service.StuService;
import com.woniuxy.provider.mapper.StuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
* @author liboyang
* @description 针对表【stu】的数据库操作Service实现
* @createDate 2024-11-29 17:19:15
*/
@Service
public class StuServiceImpl extends ServiceImpl<StuMapper, Stu> implements StuService{
    @Autowired
    private StuMapper stuMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String REDIS_KEY_PREFIX = "stu:";

    @Override
    public Page<Stu> list(int pageNum, int pageSize) {
        String redisKey = REDIS_KEY_PREFIX + pageNum + ":" + pageSize;

        Page<Stu> cachedPage = (Page<Stu>) redisTemplate.opsForValue().get(redisKey);
        if (cachedPage != null) {
            return cachedPage;
        }

        Page<Stu> page = new Page<>(pageNum, pageSize);
        Page<Stu> stuPage = stuMapper.selectPage(page, null);

        redisTemplate.opsForValue().set(redisKey, stuPage, 10, TimeUnit.MINUTES);

        return stuPage;
    }
}




